﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SafeLoad.NControl
{
	/// <summary>
	/// NTank.xaml 的交互逻辑
	/// </summary>
	public partial class NTank : UserControl
	{
		public NTank()
		{
			InitializeComponent();
		}

		//密度，温度，压力，液位高，流速
		

		/// <summary>
		/// 舱室的标题
		/// </summary>
		public static readonly DependencyProperty TankTitleProperty = DependencyProperty.Register("TankTitle",
		  typeof(string), typeof(NTank), new PropertyMetadata(""));
		public string TankTitle
		{
			get { return (string)GetValue(TankTitleProperty); }
			set { SetValue(TankTitleProperty, value); }
		}

		/// <summary>
		/// 舱室的背景颜色
		/// </summary>
		public static readonly DependencyProperty FrameBackgroundColorProperty = DependencyProperty.Register("FrameBackgroundColor",
		  typeof(Color), typeof(NTank), new PropertyMetadata(/*Colors.LightGray, */new PropertyChangedCallback(RerenderBinding)));
		public Color FrameBackgroundColor
		{
			get 
			{ 
				return (Color)GetValue(FrameBackgroundColorProperty); 
			}
			set 
			{ 
				SetValue(FrameBackgroundColorProperty, value); 
			}

		}
		/// <summary>
		/// 当前容量，单位立方米
		/// </summary>
		public static readonly DependencyProperty VolumeValueProperty = DependencyProperty.Register("VolumeValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double VolumeValue
		{
			get { return (double)GetValue(VolumeValueProperty); }
			set { SetValue(VolumeValueProperty, value); }
		}

		/// <summary>
		/// 总容量，单位立方米
		/// </summary>
		public static readonly DependencyProperty TotalVolumeProperty = DependencyProperty.Register("TotalVolume",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double TotalVolume
		{
			get { return (double)GetValue(TotalVolumeProperty); }
			set { SetValue(TotalVolumeProperty, value); }
		}

		/// <summary>
		/// 液位高度 单位 米
		/// </summary>
		public static readonly DependencyProperty LevelValueProperty = DependencyProperty.Register("LevelValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double LevelValue
		{
			get 
			{ 
				return (double)GetValue(LevelValueProperty); 
			}
			set
			{ 
				SetValue(LevelValueProperty, value); 
			}
		}

		/// <summary>
		/// 装卸速率 单位 立方米每小时
		/// </summary>
		public static readonly DependencyProperty RateValueProperty = DependencyProperty.Register("RateValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double RateValue
		{
			get { return (double)GetValue(RateValueProperty); }
			set 
			{ 
				SetValue(RateValueProperty, value); 
			}
		}

		/// <summary>
		/// 容量百分比高度
		/// </summary>
		public static readonly DependencyProperty VolumePercentHeightProperty = DependencyProperty.Register("VolumePercentHeight",
		  typeof(int), typeof(NTank), new PropertyMetadata(0));
		public int VolumePercentHeight
		{
			get { return (int)GetValue(VolumePercentHeightProperty); }
			set
			{ 
				SetValue(VolumePercentHeightProperty, value);
			}
		}

		/// <summary>
		/// 容量百分比
		/// </summary>
		public static readonly DependencyProperty VolumePercentValueProperty = DependencyProperty.Register("VolumePercentValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d));
		public double VolumePercentValue
		{
			get { return (double)GetValue(VolumePercentValueProperty); }
			set { SetValue(VolumePercentValueProperty, value); }
		}

		/// <summary>
		/// 容量的颜色
		/// </summary>
		public static readonly DependencyProperty VolumeColorProperty = DependencyProperty.Register("VolumeColor",
		  typeof(Color), typeof(NTank), new PropertyMetadata(Colors.Green));
		public Color VolumeColor
		{
			get { return (Color)GetValue(VolumeColorProperty); }
			set { SetValue(VolumeColorProperty, value); }
		}

		/// <summary>
		/// 密度
		/// </summary>
		public static readonly DependencyProperty DensityValueProperty = DependencyProperty.Register("DensityValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double DensityValue
		{
			get { return (double)GetValue(DensityValueProperty); }
			set { SetValue(DensityValueProperty, value); }
		}

		/// <summary>
		/// 当前压强值
		/// </summary>
		public static readonly DependencyProperty PressureValueProperty = DependencyProperty.Register("PressureValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double PressureValue
		{
			get { return (double)GetValue(PressureValueProperty); }
			set { SetValue(PressureValueProperty, value); }
		}

		/// <summary>
		/// 最大压强值
		/// </summary>
		public static readonly DependencyProperty MaxPressureValueProperty = DependencyProperty.Register("MaxPressureValueProperty",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double MaxPressureValue
		{
			get { return (double)GetValue(MaxPressureValueProperty); }
			set { SetValue(MaxPressureValueProperty, value); }
		}

		/// <summary>
		/// 压强图形高度
		/// </summary>
		public static readonly DependencyProperty PressureHeightValueProperty = DependencyProperty.Register("PressureHeightValue",
		  typeof(int), typeof(NTank), new PropertyMetadata(0));
		public int PressureHeightValue
		{
			get { return (int)GetValue(PressureHeightValueProperty); }
			set { SetValue(PressureHeightValueProperty, value); }
		}

		/// <summary>
		/// 当前温度
		/// </summary>
		public static readonly DependencyProperty TemperatureValueProperty = DependencyProperty.Register("TemperatureValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double TemperatureValue
		{
			get { return (double)GetValue(TemperatureValueProperty); }
			set { SetValue(TemperatureValueProperty, value); }
		}

		//public static readonly DependencyProperty BottomTemperatureValueProperty = DependencyProperty.Register("BottomTemperatureValue",
		//  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		//public double BottomTemperatureValue
		//{
		//	get { return (double)GetValue(BottomTemperatureValueProperty); }
		//	set { SetValue(BottomTemperatureValueProperty, value); }
		//}

		/// <summary>
		/// 最高温度
		/// </summary>
		public static readonly DependencyProperty MaxTemperatureValueProperty = DependencyProperty.Register("MaxTemperatureValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double MaxTemperatureValue
		{
			get { return (double)GetValue(MaxTemperatureValueProperty); }
			set { SetValue(MaxTemperatureValueProperty, value); }
		}

		/// <summary>
		/// 最低温度
		/// </summary>
		public static readonly DependencyProperty MinTemperatureValueProperty = DependencyProperty.Register("MinTemperatureValue",
		  typeof(double), typeof(NTank), new PropertyMetadata(0.0d, new PropertyChangedCallback(RerenderBinding)));
		public double MinTemperatureValue
		{
			get { return (double)GetValue(MinTemperatureValueProperty); }
			set { SetValue(MinTemperatureValueProperty, value); }
		}

		/// <summary>
		/// 温度图形高度
		/// </summary>
		public static readonly DependencyProperty TemperatureHeightValueProperty = DependencyProperty.Register("TemperatureHeightValue",
		  typeof(int), typeof(NTank), new PropertyMetadata(0));
		public int TemperatureHeightValue
		{
			get { return (int)GetValue(TemperatureHeightValueProperty); }
			set { SetValue(TemperatureHeightValueProperty, value); }
		}

		/// <summary>
		/// 压力颜色
		/// </summary>
		public static readonly DependencyProperty PressureColorProperty = DependencyProperty.Register("PressureColor",
		  typeof(Color), typeof(NTank), new PropertyMetadata(Colors.LightSeaGreen));
		public Color PressureColor
		{
			get { return (Color)GetValue(PressureColorProperty); }
			set { SetValue(PressureColorProperty, value); }
		}

		/// <summary>
		/// 温度颜色
		/// </summary>
		public static readonly DependencyProperty TemperatureColorProperty = DependencyProperty.Register("TemperatureColor",
			typeof(Color), typeof(NTank), new PropertyMetadata(Colors.LightSalmon));
		public Color TemperatureColor
		{
			get { return (Color)GetValue(TemperatureColorProperty); }
			set { SetValue(TemperatureColorProperty, value); }
		}

		/// <summary>
		/// 装载率报警灯的颜色
		/// </summary>
		public static readonly DependencyProperty VolumeAlarmColorProperty = DependencyProperty.Register("VolumeAlarmColor",
		  typeof(Color), typeof(NTank), new PropertyMetadata(Colors.GreenYellow));
		public Color VolumeAlarmColor
		{
			get { return (Color)GetValue(VolumeAlarmColorProperty); }
			set { SetValue(VolumeAlarmColorProperty, value); }
		}

		/// <summary>
		/// 压力报警颜色
		/// </summary>
		public static readonly DependencyProperty PressureAlarmColorProperty = DependencyProperty.Register("PressureAlarmColor",
		  typeof(Color), typeof(NTank), new PropertyMetadata(Colors.GreenYellow));
		public Color PressureAlarmColor
		{
			get { return (Color)GetValue(PressureAlarmColorProperty); }
			set { SetValue(PressureAlarmColorProperty, value); }
		}   
		
		/// <summary>
		/// 温度报警颜色
		/// </summary>
		public static readonly DependencyProperty TemperatureAlarmColorProperty = DependencyProperty.Register("TemperatureAlarmColor",
			typeof(Color), typeof(NTank), new PropertyMetadata(Colors.GreenYellow));
		public Color TemperatureAlarmColor
		{
			get { return (Color)GetValue(TemperatureAlarmColorProperty); }
			set { SetValue(TemperatureAlarmColorProperty, value); }
		}

		/// <summary>
		/// 装卸速率报警颜色
		/// </summary>
		public static readonly DependencyProperty RateAlarmColorProperty = DependencyProperty.Register("RateAlarmColor",
			typeof(Color), typeof(NTank), new PropertyMetadata(Colors.GreenYellow));
		public Color RateAlarmColor
		{
			get { return (Color)GetValue(RateAlarmColorProperty); }
			set { SetValue(RateAlarmColorProperty, value); }
		}

		/// <summary>
		/// 报警状态，0 正常(绿色), 1 黄色， 2 橙色， 3 红色 
		/// </summary>
		public static readonly DependencyProperty AlarmStateProperty = DependencyProperty.Register("AlarmState",
			typeof(int), typeof(NTank), new PropertyMetadata(0));
		public int AlarmState
		{
			get { return (int)GetValue(AlarmStateProperty); }
			set { SetValue(AlarmStateProperty, value); }
		}

		/// <summary>
		/// 状态显示的颜色
		/// </summary>
		public static readonly DependencyProperty AlarmStateColorProperty = DependencyProperty.Register("AlarmStateColor",
			typeof(Color), typeof(NTank), new PropertyMetadata(Colors.Green));
		public Color AlarmStateColor
		{
			get { return (Color)GetValue(AlarmStateColorProperty); }
			set { SetValue(AlarmStateColorProperty, value); }
		}

		private static void RerenderBinding(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			NTank gauge = d as NTank;
			gauge.TankUpdate();
		}

		private void TankUpdate()
		{
			double percent = 0;

			//计算容量百分比
			if (VolumeValue > 0.000001 && TotalVolume > 0.000001)
			{
				percent = this.VolumeValue / TotalVolume;
				percent = Math.Min(percent, 1);
				percent = Math.Max(percent, 0);
			}
			double oldVolumePercentValue = VolumePercentValue;
			VolumePercentValue = percent * 100;
			VolumePercentHeight = (int)(percent * (RecPercentContainer.Height - 2));

			//计算温度百分比
			double temp_percent = 0;
			if (MaxTemperatureValue - MinTemperatureValue > 0.000001)
			{
				temp_percent = (TemperatureValue - MinTemperatureValue)
				  / (MaxTemperatureValue - MinTemperatureValue);
				temp_percent = Math.Min(temp_percent, 1);
				temp_percent = Math.Max(temp_percent, 0);
			}
			TemperatureHeightValue = (int)(temp_percent * (RecTemperatureContainer.Height - 2));

			//计算压强百分比
			double press_percent = 0;
			if (MaxPressureValue > 0.000001)
			{
				press_percent = PressureValue / MaxPressureValue;
				press_percent = Math.Min(press_percent, 1);
				press_percent = Math.Max(press_percent, 0);
			}
			PressureHeightValue = (int)(press_percent * (RecPressureContainer.Height - 2));

			FrameBackgroundColor = Colors.LightSalmon;

			if (VolumePercentValue > 90)
				VolumeAlarmColor = Colors.Red;
			if(temp_percent > 0.9)
				TemperatureAlarmColor = Colors.Red;
			if (press_percent > 0.9)
				PressureAlarmColor = Colors.Red;
			if(TotalVolume > 0 && RateValue >= TotalVolume)
				RateAlarmColor = Colors.Red;
		}

		public void DensityButton_Click(object sender, RoutedEventArgs e)
		{
			OnDensityButtonClick?.Invoke(this);
		}

		public delegate void DensityButtonClick(NTank tank);
		public event DensityButtonClick OnDensityButtonClick;
	}
}
